package com.ruoyi.system.service.impl;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.NumberUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.ListProductDownLoad;
import com.ruoyi.system.mapper.ListProductDownLoadMapper;
import com.ruoyi.system.service.IListProductDownLoadService;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 总成计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-11-21
 */
@Service
public class ListProductDownLoadServiceImpl implements IListProductDownLoadService
{
    private static final Logger log = LoggerFactory.getLogger(ListProductDownLoadServiceImpl.class);

    @Autowired
    private ListProductDownLoadMapper listProductDownLoadMapper;

    /**
     * 查询总成计划
     * 
     * @param id 总成计划主键
     * @return 总成计划
     */
    @Override
    public ListProductDownLoad selectListProductDownLoadById(Long id)
    {
        return listProductDownLoadMapper.selectListProductDownLoadById(id);
    }

    /**
     * 查询总成计划
     *
     * @param code 总成计划编码
     * @return 总成计划
     */
    public ListProductDownLoad selectListProductDownLoadByCode(String code)
    {
        return listProductDownLoadMapper.selectListProductDownLoadByCode(code);
    }

    /**
     * 查询总成计划列表
     * 
     * @param listProductDownLoad 总成计划
     * @return 总成计划
     */
    @Override
    public List<ListProductDownLoad> selectListProductDownLoadList(ListProductDownLoad listProductDownLoad)
    {
        return listProductDownLoadMapper.selectListProductDownLoadList(listProductDownLoad);
    }

    /**
     * 根据生产线代码、批次号查询生产队列批次
     *
     * @param lineCode 生产线代码
     * @param batchNumber 批次号
     * @return 总成计划集合
     */
    public List<ListProductDownLoad> searchListProductDownLoad(@Param("lineCode") String lineCode, @Param("batchNumber") String batchNumber)
    {
        return listProductDownLoadMapper.searchListProductDownLoad(lineCode,batchNumber);
    }

    /**
     * 根据批次号查询批次单下车型
     *
     * @param batchNumber 批次号
     * @return 总成计划集合
     */
    public List<ListProductDownLoad> searchListProductDownLoadDetails(@Param("batchNumber") String batchNumber)
    {
        return listProductDownLoadMapper.searchListProductDownLoadDetails(batchNumber);
    }

    /**
     * 根据产线号,获取在线队列
     *
     * @param productLineCode 产线号
     * @return 总成计划集合
     */
    public List<ListProductDownLoad> getListProductDownload(@Param("productLineCode") String productLineCode)
    {
        return listProductDownLoadMapper.getListProductDownload(productLineCode);
    }

    /**
     * 新增总成计划
     * 
     * @param listProductDownLoad 总成计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertListProductDownLoad(ListProductDownLoad listProductDownLoad)
    {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dateNowStr = sdf.format(d);

        listProductDownLoad.setCreateTime(DateUtils.getNowDate());
        listProductDownLoad.setCreateBy(SecurityUtils.getUsername());
        listProductDownLoad.setStatus(UserConstants.NORMAL);
        //获取最新编号
        ListProductDownLoad lpd = listProductDownLoadMapper.findNumber();
        String sortNo = "";
        String batchNo= "";
        if(lpd != null){
            String sdate = lpd.getSortid().substring(14,22);
            String bdate = lpd.getBatchNumber().substring(14,22);
            if(sdate.equals(dateNowStr)){
                sortNo = lpd.getSortid().substring(22);
            }
            if(bdate.equals(dateNowStr)){
                batchNo = lpd.getBatchNumber().substring(22);
            }
        }
        String sno = NumberUtil.getNewEquipmentNo(Constants.ProductManualS,dateNowStr,sortNo);
        String bno = NumberUtil.getNewEquipmentNo(Constants.ProductManualB,dateNowStr,batchNo);
        listProductDownLoad.setSortid(sno);
        listProductDownLoad.setBatchNumber(bno);
        return listProductDownLoadMapper.insertListProductDownLoad(listProductDownLoad);
    }

    /**
     * 修改总成计划
     * 
     * @param listProductDownLoad 总成计划
     * @return 结果
     */
    @Override
    @Transactional
    public int updateListProductDownLoad(ListProductDownLoad listProductDownLoad)
    {
        listProductDownLoad.setUpdateTime(DateUtils.getNowDate());
        listProductDownLoad.setUpdateBy(SecurityUtils.getUsername());
        return listProductDownLoadMapper.updateListProductDownLoad(listProductDownLoad);
    }

    /**
     * 批量删除总成计划
     *
     * @param ids 需要删除的总成计划主键
     * @return 结果
     */
    @Override
    public int deleteListProductDownLoadByIds(Long[] ids) {
        return listProductDownLoadMapper.deleteListProductDownLoadByIds(ids);
    }

    /**
     * 删除总成计划信息
     * 
     * @param id 总成计划主键
     * @return 结果
     */
    @Override
    public int deleteListProductDownLoadById(Long id)
    {
        return listProductDownLoadMapper.deleteListProductDownLoadById(id);
    }

    /**
     * 导入总成计划数据
     *
     * @param listProductDownLoadList 总成计划数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importListProductDownLoad(List<ListProductDownLoad> listProductDownLoadList, boolean isUpdateSupport, String operName) {

        if (StringUtils.isNull(listProductDownLoadList) || listProductDownLoadList.size() == 0)
        {
            throw new CustomException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        //String password = configService.selectConfigByKey("sys.user.initPassword");
        for (ListProductDownLoad listProductDownLoad : listProductDownLoadList)
        {
            try
            {
                // 验证是否存在这个总成计划
                ListProductDownLoad sa = listProductDownLoadMapper.selectListProductDownLoadByCode(listProductDownLoad.getModelCode());
                if (StringUtils.isNull(sa))
                {
                    listProductDownLoad.setCreateBy(operName);
                    this.insertListProductDownLoad(listProductDownLoad);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、总成计划 " + listProductDownLoad.getModelCode() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    listProductDownLoad.setUpdateBy(operName);
                    this.updateListProductDownLoad(listProductDownLoad);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、总成计划 " + listProductDownLoad.getModelCode() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、总成计划 " + listProductDownLoad.getModelCode() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、总成计划 " + listProductDownLoad.getModelCode() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

}
